<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Python数据分析课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Pandas的函数应用 | Python数据分析课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-tbfed-pagefooter/footer.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../file/part03/3.5.html" />
    
    
    <link rel="prev" href="../../file/part03/3.3.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.4"
        data-chapter-title="Pandas的函数应用"
        data-filepath="file/part03/3.4.md"
        data-basepath="../.."
        data-revision="Thu Apr 27 2017 00:50:19 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        传智播客Python学院数据分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="file/part01/1.html">
            
                
                    <a href="../../file/part01/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        一、工作环境准备及数据分析建模理论基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="file/part01/1.1.html">
            
                
                    <a href="../../file/part01/1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Python 3.x新特性和编码回顾
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="file/part01/1.2.html">
            
                
                    <a href="../../file/part01/1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        DIKW模型与数据工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="file/part01/1.3.html">
            
                
                    <a href="../../file/part01/1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        数据分析建模理论基础
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="file/part02/2.html">
            
                
                    <a href="../../file/part02/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        二、科学计算工具NumPy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="file/part02/2.1.html">
            
                
                    <a href="../../file/part02/2.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        ndarray的创建与数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="file/part02/2.2.html">
            
                
                    <a href="../../file/part02/2.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        ndarray的矩阵处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="file/part02/2.3.html">
            
                
                    <a href="../../file/part02/2.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        ndarray的元素处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="file/part02/2.4.html">
            
                
                    <a href="../../file/part02/2.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        实战案例：2016美国总统大选民意调查统计
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="file/part03/3.html">
            
                
                    <a href="../../file/part03/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        三、数据分析工具Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="file/part03/3.1.html">
            
                
                    <a href="../../file/part03/3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Pandas的数据结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="file/part03/3.2.html">
            
                
                    <a href="../../file/part03/3.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Pandas的索引操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="file/part03/3.3.html">
            
                
                    <a href="../../file/part03/3.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Pandas的对齐运算
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="file/part03/3.4.html">
            
                
                    <a href="../../file/part03/3.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Pandas的函数应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="file/part03/3.5.html">
            
                
                    <a href="../../file/part03/3.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        Pandas的层级索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="file/part03/3.6.html">
            
                
                    <a href="../../file/part03/3.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        Pandas统计计算和描述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="file/part03/3.7.html">
            
                
                    <a href="../../file/part03/3.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        Pandas分组与聚合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="file/part03/3.8.html">
            
                
                    <a href="../../file/part03/3.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        数据清洗、合并、转化和重构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="file/part03/3.9.html">
            
                
                    <a href="../../file/part03/3.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        聚类模型 -- K-Means介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="file/part03/3.10.html">
            
                
                    <a href="../../file/part03/3.10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        实战案例：全球食品数据分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="file/part04/4.html">
            
                
                    <a href="../../file/part04/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        四、数据可视化工具
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="file/part04/4.1.html">
            
                
                    <a href="../../file/part04/4.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Matplotlib绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="file/part04/4.2.html">
            
                
                    <a href="../../file/part04/4.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        Seaborn绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="file/part04/4.3.html">
            
                
                    <a href="../../file/part04/4.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Bokeh绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="file/part04/4.4.html">
            
                
                    <a href="../../file/part04/4.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        实战案例：世界高峰数据可视化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="file/part06/6.html">
            
                
                    <a href="../../file/part06/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        五、自然语言处理NLTK
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="file/part06/6.1.html">
            
                
                    <a href="../../file/part06/6.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        NLTK与自然语言处理基础
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="file/part06/6.2.html">
            
                
                    <a href="../../file/part06/6.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        jieba分词
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="file/part06/6.3.html">
            
                
                    <a href="../../file/part06/6.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        情感分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="file/part06/6.4.html">
            
                
                    <a href="../../file/part06/6.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        文本相似度和分类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="file/part06/6.6.html">
            
                
                    <a href="../../file/part06/6.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        实战案例：微博情感分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Python数据分析课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="pandas&#x7684;&#x51FD;&#x6570;&#x5E94;&#x7528;">Pandas&#x7684;&#x51FD;&#x6570;&#x5E94;&#x7528;</h1>
<blockquote>
<h2 id="apply-&#x548C;-applymap">apply &#x548C; applymap</h2>
</blockquote>
<h4 id="1-&#x53EF;&#x76F4;&#x63A5;&#x4F7F;&#x7528;numpy&#x7684;&#x51FD;&#x6570;">1. &#x53EF;&#x76F4;&#x63A5;&#x4F7F;&#x7528;NumPy&#x7684;&#x51FD;&#x6570;</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># Numpy ufunc &#x51FD;&#x6570;</span>
df = pd.DataFrame(np.random.randn(<span class="hljs-number">5</span>,<span class="hljs-number">4</span>) - <span class="hljs-number">1</span>)
print(df)

print(np.abs(df))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">          <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>         <span class="hljs-number">3</span>
<span class="hljs-number">0</span> -<span class="hljs-number">0.062413</span>  <span class="hljs-number">0.844813</span> -<span class="hljs-number">1.853721</span> -<span class="hljs-number">1.980717</span>
<span class="hljs-number">1</span> -<span class="hljs-number">0.539628</span> -<span class="hljs-number">1.975173</span> -<span class="hljs-number">0.856597</span> -<span class="hljs-number">2.612406</span>
<span class="hljs-number">2</span> -<span class="hljs-number">1.277081</span> -<span class="hljs-number">1.088457</span> -<span class="hljs-number">0.152189</span>  <span class="hljs-number">0.530325</span>
<span class="hljs-number">3</span> -<span class="hljs-number">1.356578</span> -<span class="hljs-number">1.996441</span>  <span class="hljs-number">0.368822</span> -<span class="hljs-number">2.211478</span>
<span class="hljs-number">4</span> -<span class="hljs-number">0.562777</span>  <span class="hljs-number">0.518648</span> -<span class="hljs-number">2.007223</span>  <span class="hljs-number">0.059411</span>

          <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>         <span class="hljs-number">3</span>
<span class="hljs-number">0</span>  <span class="hljs-number">0.062413</span>  <span class="hljs-number">0.844813</span>  <span class="hljs-number">1.853721</span>  <span class="hljs-number">1.980717</span>
<span class="hljs-number">1</span>  <span class="hljs-number">0.539628</span>  <span class="hljs-number">1.975173</span>  <span class="hljs-number">0.856597</span>  <span class="hljs-number">2.612406</span>
<span class="hljs-number">2</span>  <span class="hljs-number">1.277081</span>  <span class="hljs-number">1.088457</span>  <span class="hljs-number">0.152189</span>  <span class="hljs-number">0.530325</span>
<span class="hljs-number">3</span>  <span class="hljs-number">1.356578</span>  <span class="hljs-number">1.996441</span>  <span class="hljs-number">0.368822</span>  <span class="hljs-number">2.211478</span>
<span class="hljs-number">4</span>  <span class="hljs-number">0.562777</span>  <span class="hljs-number">0.518648</span>  <span class="hljs-number">2.007223</span>  <span class="hljs-number">0.059411</span>
</code></pre>
<h4 id="2-&#x901A;&#x8FC7;apply&#x5C06;&#x51FD;&#x6570;&#x5E94;&#x7528;&#x5230;&#x5217;&#x6216;&#x884C;&#x4E0A;">2. &#x901A;&#x8FC7;apply&#x5C06;&#x51FD;&#x6570;&#x5E94;&#x7528;&#x5230;&#x5217;&#x6216;&#x884C;&#x4E0A;</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4F7F;&#x7528;apply&#x5E94;&#x7528;&#x884C;&#x6216;&#x5217;&#x6570;&#x636E;</span>
<span class="hljs-comment">#f = lambda x : x.max()</span>
print(df.apply(<span class="hljs-keyword">lambda</span> x : x.max()))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-number">0</span>   -<span class="hljs-number">0.062413</span>
<span class="hljs-number">1</span>    <span class="hljs-number">0.844813</span>
<span class="hljs-number">2</span>    <span class="hljs-number">0.368822</span>
<span class="hljs-number">3</span>    <span class="hljs-number">0.530325</span>
dtype: float64
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#x6307;&#x5B9A;&#x8F74;&#x7684;&#x65B9;&#x5411;&#xFF0C;&#x9ED8;&#x8BA4;axis=0&#xFF0C;&#x65B9;&#x5411;&#x662F;&#x5217;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x6307;&#x5B9A;&#x8F74;&#x65B9;&#x5411;&#xFF0C;axis=1&#xFF0C;&#x65B9;&#x5411;&#x662F;&#x884C;</span>
print(df.apply(<span class="hljs-keyword">lambda</span> x : x.max(), axis=<span class="hljs-number">1</span>))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-number">0</span>    <span class="hljs-number">0.844813</span>
<span class="hljs-number">1</span>   -<span class="hljs-number">0.539628</span>
<span class="hljs-number">2</span>    <span class="hljs-number">0.530325</span>
<span class="hljs-number">3</span>    <span class="hljs-number">0.368822</span>
<span class="hljs-number">4</span>    <span class="hljs-number">0.518648</span>
dtype: float64
</code></pre>
<h4 id="3-&#x901A;&#x8FC7;applymap&#x5C06;&#x51FD;&#x6570;&#x5E94;&#x7528;&#x5230;&#x6BCF;&#x4E2A;&#x6570;&#x636E;&#x4E0A;">3. &#x901A;&#x8FC7;applymap&#x5C06;&#x51FD;&#x6570;&#x5E94;&#x7528;&#x5230;&#x6BCF;&#x4E2A;&#x6570;&#x636E;&#x4E0A;</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4F7F;&#x7528;applymap&#x5E94;&#x7528;&#x5230;&#x6BCF;&#x4E2A;&#x6570;&#x636E;</span>
f2 = <span class="hljs-keyword">lambda</span> x : <span class="hljs-string">&apos;%.2f&apos;</span> % x
print(df.applymap(f2))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">       <span class="hljs-number">0</span>      <span class="hljs-number">1</span>      <span class="hljs-number">2</span>      <span class="hljs-number">3</span>
<span class="hljs-number">0</span>  -<span class="hljs-number">0.06</span>   <span class="hljs-number">0.84</span>  -<span class="hljs-number">1.85</span>  -<span class="hljs-number">1.98</span>
<span class="hljs-number">1</span>  -<span class="hljs-number">0.54</span>  -<span class="hljs-number">1.98</span>  -<span class="hljs-number">0.86</span>  -<span class="hljs-number">2.61</span>
<span class="hljs-number">2</span>  -<span class="hljs-number">1.28</span>  -<span class="hljs-number">1.09</span>  -<span class="hljs-number">0.15</span>   <span class="hljs-number">0.53</span>
<span class="hljs-number">3</span>  -<span class="hljs-number">1.36</span>  -<span class="hljs-number">2.00</span>   <span class="hljs-number">0.37</span>  -<span class="hljs-number">2.21</span>
<span class="hljs-number">4</span>  -<span class="hljs-number">0.56</span>   <span class="hljs-number">0.52</span>  -<span class="hljs-number">2.01</span>   <span class="hljs-number">0.06</span>
</code></pre>
<blockquote>
<h2 id="&#x6392;&#x5E8F;">&#x6392;&#x5E8F;</h2>
</blockquote>
<h4 id="1-&#x7D22;&#x5F15;&#x6392;&#x5E8F;">1. &#x7D22;&#x5F15;&#x6392;&#x5E8F;</h4>
<blockquote>
<p>sort_index()</p>
<p>&#x6392;&#x5E8F;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;&#x5347;&#x5E8F;&#x6392;&#x5E8F;&#xFF0C;ascending=False &#x4E3A;&#x964D;&#x5E8F;&#x6392;&#x5E8F;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># Series</span>
s4 = pd.Series(range(<span class="hljs-number">10</span>, <span class="hljs-number">15</span>), index = np.random.randint(<span class="hljs-number">5</span>, size=<span class="hljs-number">5</span>))
print(s4)

<span class="hljs-comment"># &#x7D22;&#x5F15;&#x6392;&#x5E8F;</span>
s4.sort_index() <span class="hljs-comment"># 0 0 1 3 3</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-number">0</span>    <span class="hljs-number">10</span>
<span class="hljs-number">3</span>    <span class="hljs-number">11</span>
<span class="hljs-number">1</span>    <span class="hljs-number">12</span>
<span class="hljs-number">3</span>    <span class="hljs-number">13</span>
<span class="hljs-number">0</span>    <span class="hljs-number">14</span>
dtype: int64

<span class="hljs-number">0</span>    <span class="hljs-number">10</span>
<span class="hljs-number">0</span>    <span class="hljs-number">14</span>
<span class="hljs-number">1</span>    <span class="hljs-number">12</span>
<span class="hljs-number">3</span>    <span class="hljs-number">11</span>
<span class="hljs-number">3</span>    <span class="hljs-number">13</span>
dtype: int64
</code></pre>
<blockquote>
<p>&#x5BF9;DataFrame&#x64CD;&#x4F5C;&#x65F6;&#x6CE8;&#x610F;&#x8F74;&#x65B9;&#x5411;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># DataFrame</span>
df4 = pd.DataFrame(np.random.randn(<span class="hljs-number">3</span>, <span class="hljs-number">5</span>), 
                   index=np.random.randint(<span class="hljs-number">3</span>, size=<span class="hljs-number">3</span>),
                   columns=np.random.randint(<span class="hljs-number">5</span>, size=<span class="hljs-number">5</span>))
print(df4)

df4_isort = df4.sort_index(axis=<span class="hljs-number">1</span>, ascending=<span class="hljs-keyword">False</span>)
print(df4_isort) <span class="hljs-comment"># 4 2 1 1 0</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">          <span class="hljs-number">1</span>         <span class="hljs-number">4</span>         <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>
<span class="hljs-number">2</span> -<span class="hljs-number">0.416686</span> -<span class="hljs-number">0.161256</span>  <span class="hljs-number">0.088802</span> -<span class="hljs-number">0.004294</span>  <span class="hljs-number">1.164138</span>
<span class="hljs-number">1</span> -<span class="hljs-number">0.671914</span>  <span class="hljs-number">0.531256</span>  <span class="hljs-number">0.303222</span> -<span class="hljs-number">0.509493</span> -<span class="hljs-number">0.342573</span>
<span class="hljs-number">1</span>  <span class="hljs-number">1.988321</span> -<span class="hljs-number">0.466987</span>  <span class="hljs-number">2.787891</span> -<span class="hljs-number">1.105912</span>  <span class="hljs-number">0.889082</span>

          <span class="hljs-number">4</span>         <span class="hljs-number">2</span>         <span class="hljs-number">1</span>         <span class="hljs-number">1</span>         <span class="hljs-number">0</span>
<span class="hljs-number">2</span> -<span class="hljs-number">0.161256</span>  <span class="hljs-number">1.164138</span> -<span class="hljs-number">0.416686</span> -<span class="hljs-number">0.004294</span>  <span class="hljs-number">0.088802</span>
<span class="hljs-number">1</span>  <span class="hljs-number">0.531256</span> -<span class="hljs-number">0.342573</span> -<span class="hljs-number">0.671914</span> -<span class="hljs-number">0.509493</span>  <span class="hljs-number">0.303222</span>
<span class="hljs-number">1</span> -<span class="hljs-number">0.466987</span>  <span class="hljs-number">0.889082</span>  <span class="hljs-number">1.988321</span> -<span class="hljs-number">1.105912</span>  <span class="hljs-number">2.787891</span>
</code></pre>
<h4 id="2-&#x6309;&#x503C;&#x6392;&#x5E8F;">2. &#x6309;&#x503C;&#x6392;&#x5E8F;</h4>
<blockquote>
<p>sort_values(by=&apos;column name&apos;)</p>
<p>&#x6839;&#x636E;&#x67D0;&#x4E2A;&#x552F;&#x4E00;&#x7684;&#x5217;&#x540D;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5176;&#x4ED6;&#x76F8;&#x540C;&#x5217;&#x540D;&#x5219;&#x62A5;&#x9519;&#x3002;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x6309;&#x503C;&#x6392;&#x5E8F;</span>
df4_vsort = df4.sort_values(by=<span class="hljs-number">0</span>, ascending=<span class="hljs-keyword">False</span>)
print(df4_vsort)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">          <span class="hljs-number">1</span>         <span class="hljs-number">4</span>         <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>
<span class="hljs-number">1</span>  <span class="hljs-number">1.988321</span> -<span class="hljs-number">0.466987</span>  <span class="hljs-number">2.787891</span> -<span class="hljs-number">1.105912</span>  <span class="hljs-number">0.889082</span>
<span class="hljs-number">1</span> -<span class="hljs-number">0.671914</span>  <span class="hljs-number">0.531256</span>  <span class="hljs-number">0.303222</span> -<span class="hljs-number">0.509493</span> -<span class="hljs-number">0.342573</span>
<span class="hljs-number">2</span> -<span class="hljs-number">0.416686</span> -<span class="hljs-number">0.161256</span>  <span class="hljs-number">0.088802</span> -<span class="hljs-number">0.004294</span>  <span class="hljs-number">1.164138</span>
</code></pre>
<h2 id="&#x5904;&#x7406;&#x7F3A;&#x5931;&#x6570;&#x636E;">&#x5904;&#x7406;&#x7F3A;&#x5931;&#x6570;&#x636E;</h2>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python">df_data = pd.DataFrame([np.random.randn(<span class="hljs-number">3</span>), [<span class="hljs-number">1.</span>, <span class="hljs-number">2.</span>, np.nan],
                       [np.nan, <span class="hljs-number">4.</span>, np.nan], [<span class="hljs-number">1.</span>, <span class="hljs-number">2.</span>, <span class="hljs-number">3.</span>]])
print(df_data.head())
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">          <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>
<span class="hljs-number">0</span> -<span class="hljs-number">0.281885</span> -<span class="hljs-number">0.786572</span>  <span class="hljs-number">0.487126</span>
<span class="hljs-number">1</span>  <span class="hljs-number">1.000000</span>  <span class="hljs-number">2.000000</span>       NaN
<span class="hljs-number">2</span>       NaN  <span class="hljs-number">4.000000</span>       NaN
<span class="hljs-number">3</span>  <span class="hljs-number">1.000000</span>  <span class="hljs-number">2.000000</span>  <span class="hljs-number">3.000000</span>
</code></pre>
<h4 id="1-&#x5224;&#x65AD;&#x662F;&#x5426;&#x5B58;&#x5728;&#x7F3A;&#x5931;&#x503C;&#xFF1A;isnull">1. &#x5224;&#x65AD;&#x662F;&#x5426;&#x5B58;&#x5728;&#x7F3A;&#x5931;&#x503C;&#xFF1A;isnull()</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># isnull</span>
print(df_data.isnull())
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">       <span class="hljs-number">0</span>      <span class="hljs-number">1</span>      <span class="hljs-number">2</span>
<span class="hljs-number">0</span>  <span class="hljs-keyword">False</span>  <span class="hljs-keyword">False</span>  <span class="hljs-keyword">False</span>
<span class="hljs-number">1</span>  <span class="hljs-keyword">False</span>  <span class="hljs-keyword">False</span>   <span class="hljs-keyword">True</span>
<span class="hljs-number">2</span>   <span class="hljs-keyword">True</span>  <span class="hljs-keyword">False</span>   <span class="hljs-keyword">True</span>
<span class="hljs-number">3</span>  <span class="hljs-keyword">False</span>  <span class="hljs-keyword">False</span>  <span class="hljs-keyword">False</span>
</code></pre>
<h4 id="2-&#x4E22;&#x5F03;&#x7F3A;&#x5931;&#x6570;&#x636E;&#xFF1A;dropna">2. &#x4E22;&#x5F03;&#x7F3A;&#x5931;&#x6570;&#x636E;&#xFF1A;dropna()</h4>
<blockquote>
<p>&#x6839;&#x636E;axis&#x8F74;&#x65B9;&#x5411;&#xFF0C;&#x4E22;&#x5F03;&#x5305;&#x542B;NaN&#x7684;&#x884C;&#x6216;&#x5217;&#x3002;
&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># dropna</span>
print(df_data.dropna())

print(df_data.dropna(axis=<span class="hljs-number">1</span>))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">          <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>
<span class="hljs-number">0</span> -<span class="hljs-number">0.281885</span> -<span class="hljs-number">0.786572</span>  <span class="hljs-number">0.487126</span>
<span class="hljs-number">3</span>  <span class="hljs-number">1.000000</span>  <span class="hljs-number">2.000000</span>  <span class="hljs-number">3.000000</span>

          <span class="hljs-number">1</span>
<span class="hljs-number">0</span> -<span class="hljs-number">0.786572</span>
<span class="hljs-number">1</span>  <span class="hljs-number">2.000000</span>
<span class="hljs-number">2</span>  <span class="hljs-number">4.000000</span>
<span class="hljs-number">3</span>  <span class="hljs-number">2.000000</span>
</code></pre>
<h4 id="3-&#x586B;&#x5145;&#x7F3A;&#x5931;&#x6570;&#x636E;&#xFF1A;fillna">3. &#x586B;&#x5145;&#x7F3A;&#x5931;&#x6570;&#x636E;&#xFF1A;fillna()</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># fillna</span>
print(df_data.fillna(-<span class="hljs-number">100.</span>))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">            <span class="hljs-number">0</span>         <span class="hljs-number">1</span>           <span class="hljs-number">2</span>
<span class="hljs-number">0</span>   -<span class="hljs-number">0.281885</span> -<span class="hljs-number">0.786572</span>    <span class="hljs-number">0.487126</span>
<span class="hljs-number">1</span>    <span class="hljs-number">1.000000</span>  <span class="hljs-number">2.000000</span> -<span class="hljs-number">100.000000</span>
<span class="hljs-number">2</span> -<span class="hljs-number">100.000000</span>  <span class="hljs-number">4.000000</span> -<span class="hljs-number">100.000000</span>
<span class="hljs-number">3</span>    <span class="hljs-number">1.000000</span>  <span class="hljs-number">2.000000</span>    <span class="hljs-number">3.000000</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; BigCat all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x300C;Revision Time:
2017-03-14 01:21:44&#x300D;
</span></footer>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../file/part03/3.3.html" class="navigation navigation-prev " aria-label="Previous page: Pandas的对齐运算"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../file/part03/3.5.html" class="navigation navigation-next " aria-label="Next page: Pandas的层级索引"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © BigCat","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Python数据分析课程讲义 -->
</html>
